﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Einstein.Data;
using DevComponents.DotNetBar;
using Einstein.Group.DAL;
using Einstein.Utility;
using Einstein.Group.BLL;
using DevComponents.DotNetBar.SuperGrid;

namespace Einstein.Platform.Window.Basic
{
    public partial class Basic_File : Office2007Form
    {
        static UserInfoBLL bll = new UserInfoBLL();
        public Basic_File()
        {
            InitializeComponent();
            BindFile();
        }

        #region 操作方法
        void BindFile()
        {
            dgList.PrimaryGrid.DataSource = bll.GetAllList();
            Basic_File_Edit.Enabled = false;
            Basic_File_Delete.Enabled = false;
            ModelID = null;
        }

        void NewItem()
        {
            Basic_File_New form = new Basic.Basic_File_New();
            form.Text = "新建人员档案";
            if (form.ShowDialog() == DialogResult.OK)
            {
                BindFile();
            }
        }
        void DeleteItem()
        {
            if (MyMessageBox.ShowDeleteDialog() == eTaskDialogResult.Ok)
            {
                bool result = bll.Delete(new UserInfo { UserID = ModelID.ToString() });
                if (result)
                {
                    BindFile();
                    MyMessageBox.ShowLoadAlert(this, MyMessageBox.Category.Info, "删除成功"); 
                }
                else
                {
                    MyMessageBox.ShowLoadAlert(this, MyMessageBox.Category.Warning, "删除失败");
                }
            }
        }
        void EditItem()
        {
            Basic_File_New form = new Basic.Basic_File_New();
            form.Text = "编辑人员档案";
            form.ModelID = this.ModelID;
            form.BindModel();
            if (form.ShowDialog() == DialogResult.OK)
            {
                BindFile();
            }
        }

        void ClearFilter()
        {
            GridPanel panel = dgList.PrimaryGrid;
            foreach (GridColumn column in panel.Columns)
                column.FilterExpr = null;
        }
        #endregion

        #region 事件 
        public object ModelID { get; set; }

        public void SetModelID()
        {
            UserInfo model = this.dgList.PrimaryGrid.GetModel<UserInfo>();
            Basic_File_Edit.Enabled = false;
            Basic_File_Delete.Enabled = false;
            if (model != null)
            {
                Basic_File_Edit.Enabled = true;
                Basic_File_Delete.Enabled = true;
                ModelID = model.UserID;
            }
            else
            {
                ModelID = null;
            }
        }
        /// <summary>
        /// 获取当前选择行数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgList_RowClick(object sender, DevComponents.DotNetBar.SuperGrid.GridRowClickEventArgs e)
        {
            SetModelID();
        }

        /// <summary>
        /// 右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            ButtonItem item2 = sender as ButtonItem;
            string name = item != null ? item.Name : item2.Name;

            switch (name)
            {
                case "tsmiAdd":
                case "Basic_File_New":
                    NewItem();
                    break;
                case "tsmiEdit":
                case "Basic_File_Edit":
                    EditItem();
                    break;
                case "tsmiDelete":
                case "Basic_File_Delete":
                    DeleteItem();
                    break;
                case "tsmiRefresh":
                    BindFile();
                    break;
                case "tsmiClearFilter":
                case "Basic_File_Clear":
                    ClearFilter();
                    break;
            }
        }

        /// <summary>
        /// 双击编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgList_RowDoubleClick(object sender, DevComponents.DotNetBar.SuperGrid.GridRowDoubleClickEventArgs e)
        {
            EditItem();
        } 
        private void dgList_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                SetModelID();
                tsmiDelete.Enabled = true;
                tsmiEdit.Enabled = true;
                if (ModelID == null)
                {
                    tsmiDelete.Enabled = false;
                    tsmiEdit.Enabled = false;
                }
            }
        }

        private void dgList_Click(object sender, EventArgs e)
        {
            SetModelID();
        }
        #endregion 
    }
}
